package com.eyas.business.service.common;

import com.eyas.business.config.webmvc.exception.custom.uncheck.EyasBusinessException;
import com.eyas.business.config.webmvc.interceptor.LoginInterceptor;
import com.eyas.business.model.jdbcEntity.table.KeyWorldsInfo;
import com.eyas.business.model.jpa.AdminInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

import static com.eyas.core.jdbc.utils.CommonJdbcUtils.commonJdbcUtils;

import java.util.List;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/7 17:14
 * @Description:
 */
@Service
@Transactional
public class CommonServiceImpl implements CommonService {
    @Override
    public List<KeyWorldsInfo> dictionary(String dcode) {
        List<KeyWorldsInfo> list = commonJdbcUtils.queryList("select * from keyworldsinfo where validflag='1' and dcode=?",KeyWorldsInfo.class,dcode);
        return list;
    }

    @Override
    public void deleteInfo(String tablename, String primarykey, Integer primaryval, HttpSession session) {
        if(!StringUtils.hasText(tablename))
            throw new EyasBusinessException("缺少要删除的表命名");
        if(!StringUtils.hasText(primarykey))
            throw new EyasBusinessException("缺少要删除的表的依据");
        if(primaryval==null)
            throw new EyasBusinessException("缺少要删除的数据信息");
        AdminInfo adminInfo = (AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME);
        if(adminInfo.getAdminnid()!=1)
            throw new EyasBusinessException("您没有删除数据的权限");
        commonJdbcUtils.delete("delete from "+tablename+" where "+primarykey+"=?",primaryval);
    }
}
